package com.example.zhu95.mytimber.fragments;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.example.zhu95.mytimber.R;
import com.example.zhu95.mytimber.adapters.AlbumSmallAdapter;
import com.example.zhu95.mytimber.adapters.ItemVpopupAdapter;
import com.example.zhu95.mytimber.entity.AlbumBean;
import com.example.zhu95.mytimber.entity.ArtistBean;
import com.example.zhu95.mytimber.entity.SongBean;
import com.example.zhu95.mytimber.entity.SongData;
import com.example.zhu95.mytimber.utils.Util;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import static com.example.zhu95.mytimber.ConstantValue.ARTIST_ID;

/**
 * @author zhu95
 * @date 2018/3/4
 */
public class ArtistDetailFragment extends BaseFragment {
    View view;
    Toolbar toolbar;
    ImageView imgArtistHeader;
    RecyclerView recyclerViewArtistDetail;
    RecyclerView recyclerViewArtistDetailAlbum;
    LinearLayoutManager layoutManager1;
    LinearLayoutManager layoutManager2;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        view = inflater.inflate(R.layout.fragment_artist_detail, container, false);
        toolbar = view.findViewById(R.id.toolbar);
        imgArtistHeader = view.findViewById(R.id.img_artist_header);
        recyclerViewArtistDetail = view.findViewById(R.id.recycler_view_artist_detail);
        recyclerViewArtistDetailAlbum = view.findViewById(R.id.recycler_view_artist_detail_album);
        layoutManager1 = new LinearLayoutManager(rootActivity);
        layoutManager1.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerViewArtistDetail.setLayoutManager(layoutManager1);
        layoutManager2 = new LinearLayoutManager(rootActivity);
        layoutManager2.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyclerViewArtistDetailAlbum.setLayoutManager(layoutManager2);

        rootActivity.setSupportActionBar(toolbar);
        ActionBar actionBar = rootActivity.getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
        return view;
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        menu.findItem(R.id.action_viewas).setVisible(false);
        menu.findItem(R.id.action_order_by).setVisible(false);
        menu.findItem(R.id.action_all_random).setVisible(true);
        menu.findItem(R.id.action_settings).setVisible(true);
        menu.findItem(R.id.action_search).setVisible(true).setIcon(R.drawable.ic_search_white_24dp);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                rootActivity.onBackPressed();
                return true;
            default:
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onResume() {
        super.onResume();
        Bundle bundle = getArguments();
        int artistid = bundle.getInt(ARTIST_ID);
        ArtistBean artist = SongData.findArtistByID(artistid);
        toolbar.setTitle(artist.getArtist());
        String artistArt = artist.getArtist();
        if (!TextUtils.isEmpty(artistArt)) {
            Glide.with(rootActivity).load(artistArt).centerCrop().error(R.drawable.ic_empty_music2).into(imgArtistHeader);
        } else {
            Glide.with(rootActivity).load(R.drawable.ic_empty_music2).centerCrop().into(imgArtistHeader);
        }
        imgArtistHeader.setColorFilter(getResources().getColor(Util.INSTANCE.getRandomForeColor()));

        List<SongBean> songBeans = new ArrayList<>();
        for (SongBean song : SongData.songs) {
            if (song.getArtistId() == artistid) {
                songBeans.add(song);
            }
        }
        recyclerViewArtistDetail.setAdapter(new ItemVpopupAdapter(songBeans, ItemVpopupAdapter.ItemType.ARTIST, rootActivity));

        Set<AlbumBean> albumBeans = new HashSet<>();
        for (SongBean songBean : songBeans) {
            albumBeans.add(SongData.findAlbumByID(songBean.getAlbumId()));
        }
        List<AlbumBean> albums = new ArrayList<>(albumBeans);
        recyclerViewArtistDetailAlbum.setAdapter(new AlbumSmallAdapter(albums));
    }
}
